package com.wzdigit.wms.basic.mapper.core.transfer;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wzdigit.framework.orm.mybatis.MyBatisRepository;
import com.wzdigit.wms.basic.domain.dtos.inventory.PrintLabelDto;
import com.wzdigit.wms.basic.domain.dtos.order.QtyGroupPoSkuDto;
import com.wzdigit.wms.basic.domain.dtos.transfer.GdnUnPickGroupWhsDto;
import com.wzdigit.wms.basic.domain.dtos.transfer.UpdateGdnDtlQtyDto;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.client.request.gdn.other.SearchOrderGoodsRequest;
import com.wzdigit.wms.basic.client.request.transfer.SearchGdnHeaderReportRequest;
import com.wzdigit.wms.basic.client.response.gdn.other.OtherOutOrderGoodsResponse;
import com.wzdigit.wms.basic.client.response.transfer.*;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 发货单明细,GDN=Goods Delivery Notes Mapper 接口
 * </p>
 *
 * @author
 * @since 2021-02-22
 */
@MyBatisRepository
public interface GdnDtlMapper extends BaseMapper<GdnDtl> {

	// 按仓库分组统计的未拣货数量
	List<GdnUnPickGroupWhsDto> selectUnPickGroupWhs(@Param("skuId") Integer skuId, 
			@Param("lotno") String lotno, @Param("whsIdList") List<Integer> whsIdList);
	
	// 根据来源单号查找发货单明细及未装柜数量
	// 按 仓库、料号、订单号 维护，统计 这个发货单 对应的 发货通知单下(已发货7、关闭8)的明细行数据
	List<GdnDtlAndUnshipQtyResponse> selectAllAndUnshipQtyBySourceNo(String sourceNo);
	
	List<GdnDtl> selectAllBySourceNo(String sourceNo);

	// 按【订单号、料号】分组计算出整体可余留数
	List<QtyGroupPoSkuDto> selectQtyGroupPoSku(@Param("sourceNo") String sourceNo);

	// 按【订单号、料号、仓库、来源单号】找出发货单明细
	List<GdnDtl> selectAllByPoSkuSourceNo(@Param("sourceNo") String sourceNo,
			@Param("poNo") String poNo, @Param("skuId") Integer skuId);

	Integer lockMaxGdnLineNo(@Param("gdnHeaderId") Long gdnHeaderId);

	List<GdnDtlReportResponse> selectGdnDtlReportList(@Param("gdnHeaderId") Integer gdnHeaderId,
													  @Param("closeBegin") Date closeBegin,
													  @Param("closeEnd") Date closeEnd);
	
	List<GdnHeaderAndDtlReportResponse> selectGdnHeaderAndDtlReportList(
			@Param("dto")SearchGdnHeaderReportRequest dto,
			@Param("gdnTypeList") List<String> gdnTypeList,
			@Param("beginDate") Date beginDate,
			@Param("endDate") Date endDate,
			@Param("beginCloseDate") Date beginCloseDate,
			@Param("endCloseDate") Date endCloseDate,
			@Param("gdnDtlStatus") Integer gdnDtlStatus);

	List<GdnDtlListResponse> selectAll(@Param("gdnHeaderId") Integer gdnHeaderId,
			@Param("gdnDtlStatusList")List<Integer> gdnDtlStatusList);

	List<GdnDtlListResponse> selectAllRaw(@Param("gdnHeaderId") Integer gdnHeaderId,
										  @Param("gdnDtlStatusList")List<Integer> gdnDtlStatusList);

	int updateGdnDtlQty(UpdateGdnDtlQtyDto dto);

	List<String> selectGdnList(@Param("waitload") Integer waitload, @Param("loading") Integer loading);

	List<String> selectRefCntrNoList(@Param("waitload") Integer waitload, @Param("loading") Integer loading);

	List<GdnCtnrDtlResponse> selectDtlQtyList(Integer dtlId);

	List<GdnDtlGoodsListResponse> selectDtlSnList(Integer dtlId);

	/**
	 * 根据发货通知单，物料编码，销售订单查询跨柜的发货通知单明细
	 * @param sourceShipNotice
	 * @param dtlId
	 * @return
	 */
	List<Integer> selectBySourceShipNotice(@Param("sourceShipNotice") String sourceShipNotice, @Param("soNo") String soNo, @Param("skuId") Integer skuId, @Param("dtlId") Long dtlId);

	void updateStatusAndQty(@Param("code") int code, @Param("snQty") BigDecimal snQty, @Param("gdnDtlId") Integer gdnDtlId);

	List<GdnCtnrDtlResponse> selectRemainInfo(Integer dtlId);

	int updateStatusBySourceShipNoticeAndGdnType(@Param("status") Integer status,
			@Param("sourceShipNotice") String sourceShipNotice, @Param("gdnType") String gdnType);

	void updateStatusByGdnNo(@Param("status") int status, @Param("gdnNo") String gdnNo);

	int updateByPrimaryKey(GdnDtl record);
	int emptyWhsAndPreAlocQty(@Param("udf1") String udf1, 
			@Param("modifyUser") String modifyUser, @Param("gdnDtlId") Integer gdnDtlId);
	int updateStatusById(@Param("status") Integer status, 
			@Param("modifyUser") String modifyUser, @Param("gdnDtlId") Long gdnDtlId);
    List<OtherOutOrderGoodsResponse> selectGoodsByNo(SearchOrderGoodsRequest input);

    void updateRemainQtyAndPreQty(Integer dtlId);

	String selectSourceByDtlId(Long dtlId);

	void updateLotNoAndAttr(GdnDtl gdnDtl);

    List<GdnDtl> selectAllowCreatePickTask(@Param("gdnTypeList") List<String> gdnTypeList, @Param("commonFlag") Integer commonFlag);

    List<GdnDtl> selectGdnAllotWhsTask(@Param("gdnTypeList") List<String> gdnTypeList,
    		@Param("factoryList") List<String> factoryList, @Param("rowSize") Integer rowSize);

    List<CtnrLoadingPrintListResponse> selectPrintListByGdnHeaderId(Integer gdnHeaderId);

    List<GdnDtlForIfListResponse> selectListBySourceNo(String sourceNo);

    int selectSizeBySourceNoAndStatus(@Param("sourceNo") String sourceNo, @Param("status") Integer status);

	GdnDtl selectBySoSkuIdSourceShipNotice(@Param("soNo") String soNo, @Param("skuId") Integer skuId, @Param("sourceShipNotice") String sourceShipNotice, @Param("shipmentedCode") Integer shipmentedCode, @Param("sn") String sn);

	// TODO 增加仓库校验
	GdnDtl selectBySoSkuIdSourceShipNoticeNew
			(@Param("soNo") String soNo, @Param("skuId") Integer skuId,
			 @Param("sourceShipNotice") String sourceShipNotice,
			 @Param("shipmentedCode") int shipmentedCode,
			 @Param("sn") String sn,@Param("whsId") Integer whsId);

//	List<GdnDtl> selectByHeaderId(@Param("gdnHeaderId") Integer gdnHeaderId);

	List<GdnDtl> selectByHeaderId(@Param("gdnHeaderId")
								  Long gdnHeaderId);


    int updateGdnDtlPickQty2(GdnDtl gdnDtl);
    Integer selectMaxLineId(@Param("gdnHeaderId") Long gdnHeaderId);

	List<GdnDtlListResponse> selectDtlLineNoAndSkuCodeByHeaderId(@Param("gdnHeaderId") Integer gdnHeaderId);

	int updatePickQtyByPk(@Param("gdnDtlId") Long gdnDtlId, @Param("dtlRemainQty") BigDecimal dtlRemainQty);

	List<String> selectDistinctWareHouse(@Param("gdnHeaderId") Integer gdnHeaderId);

    int updateStatusQty(@Param("code") Integer code, @Param("snQty") BigDecimal snQty, @Param("gdnDtlId") Long gdnDtlId, @Param("loginName") String loginName, @Param("date") Date date);

	int updateCloseStatusByGdnNo(@Param("status") int status, @Param("gdnNo") String gdnNo, @Param("loginName") String loginName, @Param("date") Date date);

	//查询装柜顺序集合
	List<Integer> selectCtnrSequence(@Param("gdnHeaderId") BigDecimal gdnHeaderId,@Param("gdnDtlId") BigDecimal gdnDtlId);

	//更新装柜顺序
	int updateCtnrSequence(@Param("ctnrSequence") Integer ctnrSequence,@Param("gdnDtlId") BigDecimal gdnDtlId);

	//查询发货单下完成分库的明细数量
	Integer selectSplitCoutnt(@Param("gdnHeaderId") Long gdnHeaderId,@Param("udf1") String udf1);

	//原材料【拣料】查询明细接口
    List<GdnDtl> selectRawGdnDtl(@Param("dtlIds") List<Long> dtlIds);

	List<PrintLabelDto> getLineAndBatch(@Param("headerId") BigDecimal headerId, @Param("skuCode") String skuCode);
}